package tools;
import java.io.*;
//lineWriter中的工具能帮助我们对单一文件中某一特定行的内容进行更改
public class lineWriter {
        //writeLine函数能帮助我们修改信息，括号中添加“文本路径”“想要修改的数据在第几行”以及“新的内容”
        public static void writeLine(String path,int lineNo,String newStr) {
            String temp = "";
            try {
                File file = new File(path);
                FileInputStream fis = new FileInputStream(file);
                InputStreamReader isr = new InputStreamReader(fis);
                BufferedReader br = new BufferedReader(isr);
                StringBuffer buf = new StringBuffer();

                // 保存该行前面的内容
                for (int j = 1; (temp = br.readLine()) != null; j++) {
                    if (j == lineNo) {
                        buf = buf.append(newStr);
                    } else {
                        buf = buf.append(temp);
                    }
                    buf = buf.append(System.getProperty("line.separator"));
                }

                br.close();
                FileOutputStream fos = new FileOutputStream(file);
                PrintWriter pw = new PrintWriter(fos);
                pw.write(buf.toString().toCharArray());
                pw.flush();
                pw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        //lineRewrite函数能帮助我们重新修改某一行的某一指定数据，括号中添加参数“文本路径””重写内容在第几行““旧的内容”“新的内容”
        public static String lineRewrite(String path,int lineNum,String content,String new_content){
            lineReader lr = new lineReader();
    String t1 = lr.readLine_by_number(path,lineNum);
    String[]t2 = t1.split("\\s+");
    int i=0;
    while(i<t2.length){
        if(t2[i].equals(content)){
            t2[i]=new_content;
            break;
        }
        i=i+1;
    }
    String t3=t2[0];
    int j=1;
    while(j<t2.length){
        t3=t3+" "+t2[j];
        j=j+1;
    }
    return t3;
}
    }
